package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.ConsultFiles;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.ConsultFilesService;
import co.yixiang.modules.system.dto.ConsultFilesDto;
import co.yixiang.modules.system.dto.queryCriteria.ConsultFilesQueryCriteria;
import co.yixiang.modules.system.mapper.ConsultFilesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author zf
* @date 2021-07-10
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "consultFiles")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ConsultFilesServiceImpl extends BaseServiceImpl<ConsultFilesMapper, ConsultFiles> implements ConsultFilesService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(ConsultFilesQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<ConsultFiles> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(ConsultFilesDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<ConsultFiles> queryAll(ConsultFilesQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(ConsultFiles.class, criteria));
    }


    @Override
    public void download(List<ConsultFilesDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ConsultFilesDto consultFiles : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("微信平台id", consultFiles.getOpenId());
            map.put("查档类型 1分户  2分栋", consultFiles.getFilesType());
            map.put("权利人类型 1个人 2单位", consultFiles.getObligeeType());
            map.put("产权证书类型 1房地产证  2不动产证", consultFiles.getPropertyRight());
            map.put("房地产证编号", consultFiles.getNumber());
            map.put("身份证或者姓名", consultFiles.getNameCard());
            map.put("身份证号码", consultFiles.getIdCard());
            map.put("创建时间", consultFiles.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
